package graph.drawingshapes
{
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	import graph.drawingshapes.shapes.RectangleShape;
	
	import mx.events.PropertyChangeEvent;
	
	public class ResizeMarker extends RectangleShape
	{
		public static const NAME:String = "ResizeMarker";
		public var ID:String = "no id";
		public override function get getName():String
		{
			return NAME;
		}
		
		protected var prevX:Number = 0;
		protected var prevY:Number = 0;
		
		public function ResizeMarker(id:String, x:Number=0, y:Number=0, width:Number=6, height:Number=6)
		{
			super(x, y, width, height);
			this.ID = id;
			initSize()
			initFill();
			initOutline();
			dispatchEvent(new RedrawEvent());
		}
		
		protected function initSize():void
		{
			this.width = 6;
			this.height = 6;
		}
		
		protected function initFill():void
		{
			this.fill = new SolidFill(0xFFFFFF,1);
		}
		protected function initOutline():void
		{
			this.outline = new Outline(0x328493,0.3);
		}
		
		public override function beginDrag(boundsRect:Rectangle=null):void
		{
			super.beginDrag(boundsRect);
			prevX = this.x;
			prevY = this.y;
			this.addEventListener(Event.ENTER_FRAME, onDragEnterFrame);			
		}
		
		public override function endDrag():void
		{
			super.endDrag();
			this.removeEventListener(Event.ENTER_FRAME, onDragEnterFrame);
		}
		
		private function onDragEnterFrame(event:Event):void
		{			
			if (this.x != prevX)
			{
				var eventX:PropertyChangeEvent = PropertyChangeEvent.createUpdateEvent(this, "x", prevX, this.x);
				dispatchEvent(eventX);
			}
			if (this.y != prevY)
			{
				var eventY:PropertyChangeEvent = PropertyChangeEvent.createUpdateEvent(this, "y", prevY, this.y);
				dispatchEvent(eventY);
			}
		}
		
		protected override function draw():void
		{
			var xCoef:Number = width / Math.abs(width);
			var xPos:Number = x - ((width / 2) * xCoef);
			var yCoef:Number = height / Math.abs(height);
			var yPos:Number = y - ((height / 2) * yCoef);
			this.graphics.drawRect(xPos, yPos, width, height);
		}
		
	}
}